package com.czh.subject.infra.basic.convert;

import com.czh.subject.infra.basic.entity.EsSubjectFields;
import com.czh.subject.infra.basic.entity.SubjectInfoES;
import org.apache.commons.collections4.MapUtils;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author caozaihui
 * @version 1.0
 * @since 2024/9/11 17:45
 */

public class SubjectInfoEsConverter {
    public static Map<String, Object> convertToMap(SubjectInfoES subjectInfoES) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put(EsSubjectFields.SUBJECT_NAME, subjectInfoES.getSubjectName());
        map.put(EsSubjectFields.SUBJECT_ANSWER, subjectInfoES.getSubjectAnswer());
        map.put(EsSubjectFields.SUBJECT_TYPE, subjectInfoES.getSubjectType());
        map.put(EsSubjectFields.CREATE_USER, subjectInfoES.getCreateUser());
        map.put(EsSubjectFields.CREATE_TIME, subjectInfoES.getCreateTime());
        map.put(EsSubjectFields.DOC_ID, subjectInfoES.getDocId());
        map.put(EsSubjectFields.SUBJECT_ID,subjectInfoES.getSubjectId());
        return map;
    }
    public static SubjectInfoES convertToSubjectInfoES(SearchHit searchHit) {
        Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
        if (CollectionUtils.isEmpty(sourceAsMap)){
            return null;
        }
        SubjectInfoES subjectInfoES = new SubjectInfoES();
        subjectInfoES.setDocId(MapUtils.getLong(sourceAsMap, EsSubjectFields.DOC_ID));
        subjectInfoES.setSubjectId(MapUtils.getLong(sourceAsMap,EsSubjectFields.SUBJECT_ID));
        subjectInfoES.setSubjectName(MapUtils.getString(sourceAsMap, EsSubjectFields.SUBJECT_NAME));
        subjectInfoES.setSubjectType(MapUtils.getInteger(sourceAsMap, EsSubjectFields.SUBJECT_TYPE));
        subjectInfoES.setSubjectAnswer(MapUtils.getString(sourceAsMap, EsSubjectFields.SUBJECT_ANSWER));
        subjectInfoES.setCreateUser(MapUtils.getString(sourceAsMap, EsSubjectFields.CREATE_USER));
        subjectInfoES.setCreateTime(LocalDate.parse(MapUtils.getString(sourceAsMap, EsSubjectFields.CREATE_TIME)));
        subjectInfoES.setScore(BigDecimal.valueOf(searchHit.getScore()).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP));
        //处理name高亮
        Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
        HighlightField subjectNameHighlight = highlightFields.get(EsSubjectFields.SUBJECT_NAME);
        if (Objects.nonNull(subjectNameHighlight)){
            StringBuilder subjectNameText = new StringBuilder();
            for (Text fragment : subjectNameHighlight.fragments()) {
                subjectNameText.append(fragment);
            }
            subjectInfoES.setSubjectName(subjectNameText.toString());
        }
        //处理answer高亮
        HighlightField subjectAnswerHighlight = highlightFields.get(EsSubjectFields.SUBJECT_ANSWER);
        if (Objects.nonNull(subjectAnswerHighlight)){
            StringBuilder subjectAnswerText = new StringBuilder();
            for (Text fragment : subjectAnswerHighlight.fragments()) {
                subjectAnswerText.append(fragment);
            }
            subjectInfoES.setSubjectAnswer(subjectAnswerText.toString());
        }
        return subjectInfoES;
    }
}
